const BaseConfig = require("./webpack.config");
const { merge } = require("webpack-merge");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CosBuildWebpackPlugin = require("./plugins/CosBuildWebpackPlugin");
module.exports = merge(BaseConfig, {
  //设置环境
  mode: "production",
  //兼容性
  //css兼容 --webit ,--o
  module: {
    rules: [
      //css兼容
      {
        test: /\.css$/,
        //postcss-loader 后处理器，写完css后自动去加上浏览器内核前缀，处理css兼容性
        //sass,less 预处理器
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [
                  [
                    //'autoprefixer',能够解决大部分浏览器的样式兼容问题
                    "postcss-preset-env",
                    "autoprefixer",
                  ],
                ],
              },
            },
          },
        ],
      },
      //js兼容
      {
        test: /\.m?js$/,
        //排除node_modules文件的匹配
        exclude: /(node_modules|bower_components)/,
        use: {
          //babel转换器，主要功能是将旧版浏览器或者是其他环境中将js新特征向后兼容（向低版本兼容）
          loader: "babel-loader",
          options: {
            //@babel/preset-env把高版本的ES语法兼容到ES5
            presets: ["@babel/preset-env"],
            // cacheDirectory: true,
          },
        },
      },
    ],
  },
  optimization: {
    //压缩js
    // minimize: true,
    minimizer: [
      // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
      `...`,
      new CssMinimizerPlugin(),
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "css/[contenthash:6].css",
    }),
    new CosBuildWebpackPlugin(),
  ],
});
